package exercise;

import expression.*;
import config.DifficultyConfig;
import java.util.*;

/**
 * 习题集类 - 实验内容2
 * 编写习题类中的产生一定数量习题的方法
 */
public class ExerciseSet {
    private List<Exercise> exercises;
    private DifficultyConfig config;
    private String difficulty;
    private Random random;
    
    public ExerciseSet(String difficulty) {
        this.difficulty = difficulty;
        this.config = createConfig(difficulty);
        this.random = new Random();
        this.exercises = new ArrayList<>();
        generateExercises();
    }
    
    private DifficultyConfig createConfig(String difficulty) {
        switch (difficulty.toLowerCase()) {
            case "easy":
                return new DifficultyConfig(1, 20, 0.7, 10);
            case "medium":
                return new DifficultyConfig(10, 50, 0.6, 15);
            case "hard":
                return new DifficultyConfig(30, 100, 0.5, 20);
            default:
                return new DifficultyConfig(1, 50, 0.6, 10);
        }
    }
    
    private void generateExercises() {
        int addCount = config.getAddCount();
        int subCount = config.getSubCount();
        int min = config.getMin();
        int max = config.getMax();
        
        // 生成加法题
        for (int i = 0; i < addCount; i++) {
            int a = random.nextInt(max - min + 1) + min;
            int b = random.nextInt(max - min + 1) + min;
            exercises.add(new Exercise(new AddExpression(a, b)));
        }
        
        // 生成减法题
        for (int i = 0; i < subCount; i++) {
            int a = random.nextInt(max - min + 1) + min;
            int b = random.nextInt(max - min + 1) + min;
            if (a < b) {
                int temp = a;
                a = b;
                b = temp;
            }
            exercises.add(new Exercise(new SubExpression(a, b)));
        }
        
        Collections.shuffle(exercises);
    }
    
    public void displayQuestions() {
        System.out.println("==========================================");
        System.out.println("          " + difficulty + "难度习题集");
        System.out.println("==========================================");
        System.out.println("配置: " + config.getStats());
        System.out.println("------------------------------------------");
        
        for (int i = 0; i < exercises.size(); i++) {
            System.out.printf("%2d. %s\n", i + 1, exercises.get(i).getQuestion());
        }
        System.out.println("==========================================");
    }
    
    public void displayAnswers() {
        System.out.println("==========================================");
        System.out.println("         " + difficulty + "难度参考答案");
        System.out.println("==========================================");
        
        for (int i = 0; i < exercises.size(); i++) {
            System.out.printf("%2d. %s\n", i + 1, exercises.get(i).getQuestionWithAnswer());
        }
        System.out.println("==========================================");
    }
    
    public List<Exercise> getExercises() {
        return exercises;
    }
}